package Java项目.学生管理系统;

import java.util.ArrayList;
import java.util.Scanner;

public class Text {

    //运用final定义变量为常量,防止更改
    private static final String ADD_STUDENT="1";
    private static final String DELETE_STUDENT="2";
    private static final String UPDATE_STUDENT="3";
    private static final String QUERY_STUDENT="4";
    private static final String EXIT="5";


    public static void main(String[] args) {
        //定义一个存储学生信息的集合
        ArrayList<Student> list=new ArrayList<>();
        //添加一个测试学生
        Student Text=new Student("-1","魑魅魍魉",100,"盘丝洞");
        list.add(Text);
        //循环
       loop: while (true) {                 //loop 标记外层循环,使barak不是默认跳出,而是break loop;
           System.out.println("-------欢迎来到学生管理系统-------");
           System.out.println("1:添加学生\t 2:删除学生");
           System.out.println("3:修改学生\t 4:查询学生");
           System.out.println("5:退出\t");
           System.out.print("请输入你的选择:");
           Scanner sc=new Scanner(System.in);
           String select=sc.next();
           switch (select){
               case ADD_STUDENT-> {System.out.println("添加学生");add_student(list);}
               case DELETE_STUDENT-> {System.out.println("删除学生");deleteStudent(list);}
               case UPDATE_STUDENT-> {System.out.println("修改学生");updateStudent(list);}
               case QUERY_STUDENT-> {System.out.println("查询学生");queryStudent(list);}
               case EXIT-> {System.out.println("退出");break loop;}//此处停止语句还可以,System.exit(0)表示停止虚拟机.
               default -> System.out.println("该选项不存在!,请重新输入:");
        }

    }
    }
    //1.添加学生
    public static void add_student(ArrayList<Student> s){
        System.out.println("请输入你要添加学生的id:");
        Scanner sc=new Scanner(System.in);
        String id= sc.next();
        if (id_exist(s,id)){
            System.out.println("该学生id已经存在!");
        }else {
            System.out.print("请输入要添加学生的姓名:");
            String name = sc.next();
            System.out.print("请输入要添加的学生年龄:");
            int age = sc.nextInt();
            System.out.print("请输入要添加的学生地址:");
            String address = sc.next();
            Student student = new Student(id, name, age, address);
            s.add(student);
            System.out.println("添加成功!");
        }
    }
    //2.删除学生
    public static void deleteStudent(ArrayList<Student> stu){
        System.out.println("请输入您要删除的学生id:");
        Scanner sc=new Scanner(System.in);
        String id=sc.next();
        if (id_exist(stu,id)){
            //根据学生id删除学生
            stu.remove(find(stu,id));
            System.out.println("删除成功!");
        }else {
            System.out.println("该学生不存在,删除失败!");
        }
    }

    //3.修改学生
    public static void updateStudent(ArrayList<Student> stu){
        Scanner sc=new Scanner(System.in);
        System.out.print("请输入要更改的学生的学生id:");
        String id=sc.next();
        if (id_exist(stu,id)){
            System.out.print("请输入要更改的学生姓名:");
            String new_name=sc.next();
            //获取学生对象
            Student new_stu=find(stu,id);
            new_stu.setName(new_name);
            System.out.print("请输入要更改的学生年龄:");
            int newage=sc.nextInt();
            System.out.print("请输入更改的学生地址:");
            String new_address=sc.next();
            new_stu.setAge(newage);
            new_stu.setAddress(new_address);
            System.out.println("学生信息更改成功!");
        }else {
            System.out.println("该学生不存在!");
        }
    }

    //4,查询学生
    public static void queryStudent(ArrayList<Student> stu){
        //查看集合的信息,如果长度大于1表示有学生,小于1则表示没有添加学生
        int query=stu.size();
        if (query>1){
            System.out.println("id  \t姓名  \t年龄  \t家庭住址");
            for (int i = 1; i < stu.size(); i++) {
                System.out.println(stu.get(i).getId()+" \t"+stu.get(i).getName()+
                        "   \t"+stu.get(i).getAge()+"   \t"+stu.get(i).getAddress());
            }
        }else {
            System.out.println("当前无学生信息,请添加后查询!");
        }
    }

    //定义方法判断id是否存在
    public static boolean id_exist(ArrayList<Student> stu ,String id){
        for (int i = 1; i < stu.size(); i++) {
            if (stu.get(i).getId().equals(id)){
                return true;
            }
        }
        return false;
    }
    //通过id查找对应的学生对象
    public static Student find( ArrayList<Student> stu,String id){
        for (int i = 1; i < stu.size(); i++) {
            if (stu.get(i).getId().equals(id)){
                return stu.get(i);
            }
        }
        return stu.get(0);
    }
}
